package com.jieshuibao.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jieshuibao.common.config.Global;
import com.jieshuibao.model.db.Category;
import com.jieshuibao.model.db.Duty;
import com.jieshuibao.model.db.Teacher;
import com.jieshuibao.service.CategoryService;
import com.jieshuibao.service.CounselorService;
import com.jieshuibao.service.DutyService;
import com.jieshuibao.service.TeacherService;
import com.jieshuibao.service.UserService;
import com.jieshuibao.util.StringUtils;
import com.jieshuibao.vedio.po.TeacherReq;
import com.jieshuibao.vedio.po.TeacherRes;

@Controller
@RequestMapping("teacher")
public class TeacherController{
	
	
	@Resource
	private TeacherService teacherService;
	
	@Resource
	private CounselorService counselorService;
	@Resource
	private CategoryService categoryService;
	@Resource
	private UserService userService;
	@Resource
	private DutyService dutyService;
	/**
	 * 教师详情
	 * @param req
	 * @return
	 */
	@RequestMapping(value="teacher",method=RequestMethod.POST)
	@ResponseBody
	public Object teacher(@RequestBody TeacherReq req){
		TeacherRes res = new TeacherRes();
		String teacherId = req.getTeacherId();
		if(StringUtils.isEmpty(teacherId)){
			res.setCode(Global.SYSTEMERROR);
			res.setDes("网络抖动");
			return res;
		}
		Teacher teacherById = teacherService.getTeacherById(teacherId);
		String userId = teacherById.getUserId();
		if(StringUtils.isNotEmpty(userId)){
//			Counselor counselor = counselorService.getCounselorByUuid(userId);
//			if(null != counselor){
//				Teacher teacher = new Teacher();
//				String adept = counselor.getAdept();
//				String goodAtBusi = getAdeptName(adept);
//				teacher.setGoodAtBusi(goodAtBusi);
//				teacher.setPersonBusi(counselor.getExperience());
//				String phoneNumber = counselor.getUserPhoneNumber();
//				User user = userService.getUserByPhoneNumber(phoneNumber);
//				String duties = user.getCounselorDuty();
//				String duty = getDutyName(duties);
//				teacher.setDuty(duty);
//				res.setData(teacher);
//			}
		}else{
			res.setData(teacherById);
		}
		res.setCode(Global.SUCCESS);
		return res;
	}
	@SuppressWarnings("unused")
	private String getAdeptName(String adept) {
		String adepts= "";
		String name="";
		if(adept.contains(",")){
			String[] split = adept.split(",");
			for (String str : split) {
				Category categoryById = categoryService.getCategoryById(str);
				name = categoryById.getName();
				name+=",";
				adepts += name;
			}
		}else{
			Category categoryById = categoryService.getCategoryById(adept);
			adepts = categoryById.getName();
		}
		if(adepts.charAt(adepts.length()-1)==','){
			adepts = adepts.substring(0, adepts.length()-1);
		}
		return adepts;
	}
	@SuppressWarnings("unused")
	private String getDutyName(String duties) {
		String dut= "";
		String name="";
		if(duties.contains(",")){
			String[] split = duties.split(",");
			for (String str : split) {
				Duty duty = dutyService.getDutyById(str);
				name = duty.getDutyName();
				name += ",";
				dut += name;
			}
		}else{
			Duty duty = dutyService.getDutyById(duties);
			dut = duty.getDutyName();
		}
		if(dut.charAt(dut.length()-1)==','){
			dut = dut.substring(0, dut.length()-1);
		}
		return dut;
	}
	
}
